<?php
// 简单测试页面来验证导航功能
require_once 'config/config.php';
require_once 'includes/database.php';

$db = new Database();
$categories = $db->getCategories();
usort($categories, function($a, $b) {
    return ($a['sort'] ?? 0) - ($b['sort'] ?? 0);
});
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>粘性导航测试</title>
    <link rel="stylesheet" href="<?php echo asset('css/sticky-nav.css'); ?>">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background: #f5f5f5;
        }
        .test-section {
            height: 100vh;
            padding: 50px;
            margin: 20px 0;
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .main-container {
            max-width: 1200px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="main-container">
        <h1>粘性导航测试页面</h1>

        <?php foreach ($categories as $category): ?>
            <div class="test-section" id="category-<?php echo $category['id']; ?>" data-category-id="<?php echo $category['id']; ?>">
                <h2><?php echo htmlspecialchars($category['name']); ?></h2>
                <p>这是 <?php echo htmlspecialchars($category['name']); ?> 的测试内容区域。</p>
                <p>滚动到此区域时，右侧导航应该高亮显示对应的分类项。</p>
            </div>
        <?php endforeach; ?>
    </div>

    <!-- 粘性侧边导航栏 -->
    <?php if (!empty($categories)): ?>
        <div class="sticky-nav-container sticky-nav-visible">
            <div class="sticky-nav__wrapper">
                <div class="sticky-nav__progress"></div>
                <h3 class="sticky-nav__title">快速导航</h3>
                <nav class="sticky-nav__nav">
                    <ul class="sticky-nav__list">
                        <?php foreach ($categories as $category): ?>
                            <li class="sticky-nav__item">
                                <a href="#category-<?php echo $category['id']; ?>"
                                   class="sticky-nav__link"
                                   data-tooltip="<?php echo htmlspecialchars($category['name']); ?>"
                                   title="<?php echo htmlspecialchars($category['name']); ?>">
                                    <?php
                                    // 提取分类名称的第一个字符作为缩写
                                    $categoryName = htmlspecialchars($category['name']);
                                    $shortName = substr($categoryName, 0, 1);
                                    echo $shortName;
                                    ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </nav>
                <div class="sticky-nav__back-to-top">
                    <a href="#"
                       class="sticky-nav__link"
                       data-tooltip="返回顶部"
                       title="返回顶部"
                       onclick="StickyNav.scrollToTop(); return false;">
                        <i class="fas fa-arrow-up"></i>
                    </a>
                </div>
            </div>
        </div>
    <?php endif; ?>

    <script src="<?php echo asset('js/sticky-nav.js'); ?>"></script>
    <script>
        // 手动初始化，确保页面加载完成
        document.addEventListener('DOMContentLoaded', function() {
            setTimeout(function() {
                StickyNav.init();
            }, 100);
        });
    </script>
</body>
</html>